package com.zry.study.update;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.FileProvider;

import com.tsy.sdk.myokhttp.response.DownloadResponseHandler;
import com.zry.study.R;
import com.zry.study.bean.UpdateFail;
import com.zry.study.bean.UpdateProgressBean;
import com.zry.study.bean.UpdateSuccess;
import com.zry.study.config.MyApplication;


import org.greenrobot.eventbus.EventBus;

import java.io.File;

/**
 * Created by zhangqie on 2017/6/16.
 */

public class UpdateUtils {
	private Context context;
	private MyApplication myApplication;
	private int id = 1;
	private NotificationManager mNotifyManager;
	private NotificationCompat.Builder mBuilder;
	
	public UpdateUtils(Context context, MyApplication myApplication, NotificationCompat.Builder
		                                                                 mBuilder,
	                   NotificationManager mNotifyManager) {
		this.context = context;
		this.myApplication = myApplication;
		this.mBuilder = mBuilder;
		this.mNotifyManager = mNotifyManager;
	}
	
	public void showDownloadAPK(final String url) {
		myApplication.getMyOkHttp().download()
			.url(url)
			.filePath(myApplication.getApkSavePath() + "/康易能.apk")
			.tag(context)
			.enqueue(new DownloadResponseHandler() {
				@Override
				public void onFinish(File downloadFile) {
					
					EventBus.getDefault().post(new UpdateSuccess(downloadFile.toString()));
					
//					mBuilder.setProgress(0, 0, false).setContentText("康易能下载完成");
//					mNotifyManager.notify(id, mBuilder.build());
					showSelectAPK(context);
				}
				
				@Override
				public void onProgress(long currentBytes, long totalBytes) {
					//更新进度条
					int currentNum = (int) (100*currentBytes/totalBytes);
					
					EventBus.getDefault().post(new UpdateProgressBean(currentNum));
					
//					mBuilder.setProgress(100, currentNum, false);
//					mNotifyManager.notify(id, mBuilder.build());
				}
				
				@Override
				public void onFailure(String error_msg) {
					
					EventBus.getDefault().post(new UpdateFail(error_msg));
					
					
//					mBuilder.setContentTitle("康易能下载").setContentText("下载失败")
//						.setSmallIcon(R.mipmap.ic_launcher);
				}
			});
		
		
		
	}
	
	/***
	 * 调起安装APP窗口  安装APP
	 * @param context
	 */
	private void showSelectAPK(Context context) {
		File apkPath = new File(myApplication.getApkSavePath(), "康易能.apk");
		Intent intent = new Intent(Intent.ACTION_VIEW);
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
			//参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
			Uri apkUri = FileProvider.getUriForFile(context, "com.zry.study.fileprovider",
				apkPath);
			intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
			intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
			context.startActivity(intent);
		} else {
			intent.addCategory(Intent.CATEGORY_DEFAULT);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.setDataAndType(Uri.fromFile(apkPath), "application/vnd.android" +
				                                             ".package-archive");
			context.startActivity(intent);
		}
	}
}
